﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace MVCCentral.Framework.Logger.Email
{
    class Email
    {
        #region Constructors
        public Email()
        {

        }
        #endregion

        #region Properties
        private String _ToList;

        public String ToList
        {
            get { return _ToList; }
            set { _ToList = value; }
        }

        private string _FromName;

        public string FromName
        {
            get { return _FromName; }
            set { _FromName = value; }
        }

        private string _FromEmail;

        public string FromEmail
        {
            get { return _FromEmail; }
            set { _FromEmail = value; }
        }

        private bool _isHTML;

        public bool isHTML
        {
            get { return _isHTML; }
            set { _isHTML = value; }
        }

        private string _CCList;

        public string CCList
        {
            get { return _CCList; }
            set { _CCList = value; }
        }

        private String _Subject;

        public String Subject
        {
            get { return _Subject; }
            set { _Subject = value; }
        }

        private string _MessageBody;

        public string MessageBody
        {
            get { return _MessageBody; }
            set { _MessageBody = value; }
        }


        #endregion

        /// <summary>
        /// This is the common email method.
        /// </summary>
        /// <param name="mailer"></param>
        /// <returns></returns>
        public Boolean SendEmail(Email mailer)
        {
            Boolean blnOk = false;
            SmtpClient client = new SmtpClient();

            using (MailMessage message = new MailMessage())
            {

                if (!String.IsNullOrEmpty(mailer.ToList))
                {
                    String[] toListNames = mailer.ToList.Split(new char[] { ';' });
                    foreach (string to in toListNames)
                    {
                        message.To.Add(new MailAddress(to));
                    }
                }

                if (!String.IsNullOrEmpty(mailer.CCList))
                {
                    String[] ccListNames = mailer.CCList.Split(new char[] { ';' });
                    foreach (string cc in ccListNames)
                    {
                        message.CC.Add(new MailAddress(cc));
                    }
                }

                if (mailer.FromEmail != null)
                {
                    message.From = new MailAddress(mailer.FromEmail, mailer.FromName);
                }
                message.Subject = mailer.Subject;
                message.IsBodyHtml = mailer.isHTML;
                message.Body = mailer.MessageBody;

                try
                {
                    client.Send(message);
                    blnOk = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return blnOk;
        }

    }
}
